const http = require('http');
let querystring = require('querystring');
let url = require('url');
const router = require('./router');

function createServer() {
    http.createServer(function(req, res) {
        let pathname = url.parse(req.url).pathname;
        let data = [];
        req.on('data', function(chunk) {
            data.push(chunk);
        });
        req.on('end', function() {
            let params = '';
            if (req.method === 'POST') {
                params = querystring.parse(Buffer.concat(data).toString());
            } else {
                params = url.parse(req.url, true).query;
            }
            router.router(pathname, res, params);
        }).on('error', function(err) {
            if (err) {
                console.log(err);
            }
        });
    }).listen(80);
    console.log('now is listen at 80!');
}
module.exports.createServer = createServer;
